﻿using System;
using System.Windows;
using System.Windows.Input;
using Gencode.VECore.VectorShape;

namespace Gencode.VECore.Tools
{
    public class CurveTool : Gencode.VECore.Tools.FactoryTool
    {
        protected override VECShape CreateShape(Point mousePosition)
        {
            var shape = new VECCurve(mousePosition, mousePosition);

            shape.StrokeThickness = ToolProperty.StrokeThickness;
            shape.Stroke = ToolProperty.Stroke;

            return shape;
        }

        public override void OnMouseLeftButtonUp(CanvasDocument sender, MouseButtonEventArgs e)
        {
            var shape = sender.SelectedLayer.ShapeList[0] as VECCurve;

            if (shape != null)
            {
                //reposition handle 2 and 3
                double distx = (shape.EndPoint.X - shape.StartPoint.X);
                double disty = (shape.EndPoint.Y - shape.StartPoint.Y);

                //-10% from end
                double dx = distx - ((distx / 100) * 10);
                double dy = disty - ((disty / 100) * 10);
                shape.EndCurvePoint = new Point(shape.StartPoint.X + dx, shape.StartPoint.Y + dy);

                //-10% from start
                dx = distx - ((distx / 100) * 90);
                dy = disty - ((disty / 100) * 90);
                shape.StartCurvePoint = new Point(shape.StartPoint.X + dx, shape.StartPoint.Y + dy);

                //Add shape to history
                sender.AddCommandToHistory(new Gencode.VECore.History.AddCommand(shape));
            }
        }

        public override void OnMouseMove(CanvasDocument sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point m = Mouse.GetPosition(sender);

                var shape = sender.SelectedLayer.ShapeList[0] as VECCurve;
                if (shape != null)
                    shape.MoveHandle(m, 4);
            }
        }
    }
}
